package com.eatme.web.app.article.service.impl;

import com.eatme.web.app.article.service.EmailService;
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.io.IOException;

@ConditionalOnProperty(prefix = "eatme.email-provider", name = "name", havingValue = "api")
@Service
public class EmailServiceApiImpl implements EmailService {

    private static final Logger LOG = LoggerFactory.getLogger(EmailServiceApiImpl.class);

    // private SendGrid sendGrid;

    @Override
    public void send(String email, String msg) {
        Email from = new Email("815101873@qq.com");
        String subject = "eatme-web Spring Security 登录验证码";
        Email to = new Email(email);
        Content content = new Content("text/plain", "验证码为:" + msg);
        Mail mail = new Mail(from, subject, to, content);
        Request request = new Request();
        try {
            request.setMethod(Method.POST);
            request.setEndpoint("mail/send");
            request.setBody(mail.build());
//            Response response = sendGrid.api(request);
//            if (response.getStatusCode() == 202) {
//                LOG.info("邮件发送成功");
//            } else {
//                LOG.error(response.getBody());
//            }
        } catch (IOException e) {
            LOG.error("请求发生异常 {}", e.getLocalizedMessage());
        }
    }
}
